cc.Class({
    extends: cc.Component,

    properties: {
        bg_sp: {
            default: null,
            type: cc.Sprite,
            displayName: '面板背景Sprite'
        },
        head_sp: {
            default: null,
            type: cc.Sprite,
            displayName: '图片信息Sprite'
        },
        frame_sp: {
            default: null,
            type: cc.Sprite,
            displayName: '框Sprite'
        },
        info_lab: {
            default: null,
            type: cc.Label,
            displayName: '提示信息Label'
        },
        //检测长按时间
        holdTime: {
            default: 0,
            visible: false
        },
        //检测点击
        holdClick: {
            default: false,
            visible: false
        },
        //检测双击
        doubleClick: {
            default: 0,
            visible: false
        },
        //检测单击
        hold_one_click: {
            default: 0,
            visible: false
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    /**
     * 
     * @param {*} bgUrl 背景资源url
     * @param {*} bgName 背景图片名字
     * @param {*} iconUrl icon资源url
     * @param {*} iconName icon资源名字
     * @param {*} frameUrl 框的资源url
     * @param {*} frameName 框的资源名字
     * @param {*} scale 节点的大小
     */
    setItemInfo(bgUrl, bgName, iconUrl, iconName, frameUrl, frameName, scale) {
        if (bgUrl && frameName && this.bg_sp) {
            this.bg_sp.spriteFrame = he.load.get(`${bgUrl}`, cc.SpriteAtlas).getSpriteFrame(bgName)
        } else {
            this.bg_sp.node.active = false
        }
        if (iconUrl && frameName && this.head_sp) {
            this.head_sp.spriteFrame = he.load.get(`${iconUrl}`, cc.SpriteAtlas).getSpriteFrame(iconName)
        } else {
            this.head_sp.node.active = false
        }
        if (frameUrl && frameName && this.head_sp) {
            this.frame_sp.spriteFrame = he.load.get(`${frameUrl}`, cc.SpriteAtlas).getSpriteFrame(frameName)
        } else {
            this.head_sp.node.active = false
        }
        if (scale) {
            this.bg_sp.node.setScale(scale)
            this.head_sp.node.setScale(scale)
            this.frame_sp.node.setScale(scale)
        }
    },

    // setIcon(iconUrl, iconName) {
    //     if (iconUrl && frameName && this.head_sp) {
    //         this.head_sp.spriteFrame = he.load.get(`${iconUrl}`, cc.SpriteAtlas).getSpriteFrame(iconName)
    //     }
    // },


    /**
     * 
     * @param {*} size 字体大小
     * @param {*} pos lab的位置
     * @param {*} color 字体颜色
     */
    setLabInfo(size, pos, color) {
        if (!this.info_lab) {
            he.log('lab未绑定')
            return
        } else {
            let lab = this.info_lab.node
            lab.active = true
            if (size) {
                this.info_lab.fontSize = size
            }
            if (pos) {
                lab.setPosition(pos)
            }
            if (color) {
                lab.setColor(color)
            }
        }
    },


    // 开启监听，如果是拖动的touch事件 需要自行重新设计，不用开启该监听
    openButtonListen(startfunc, oncefunc, doublefunc, longfunc) {
        if (startfunc) {
            this.startFunc = startfunc
        }
        if (oncefunc) {
            this.onceFunc = oncefunc
        }
        if (doublefunc) {
            this.doubleFunc = doublefunc
        }
        if (longfunc) {
            this.longFunc = longfunc
        }

        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            this.holdClick = true;
            this.holdTime = 0;
        }, this);

        this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
            this.holdClick = false;
            if (this.holdTime >= 60) {
                this.btn_status('long');
            }
            else {
                this.btn_status('short');
            }
            //开始记录时间
            this.holdTime = 0;
        }, this);
    },
    btn_status(status) {
        if (status == 'short') {
            this.hold_one_click++;
            this.setTime = setTimeout(() => {
                if (this.hold_one_click == 1 && this.onceFunc) {
                    this.onceFunc()
                    this.hold_one_click = 0;
                }
                else if (this.hold_one_click == 2 && this.doubleFunc) {
                    this.doubleFunc()
                    this.hold_one_click = 0;
                }
            }, 400);
        }
        else {
            this.hold_one_click = 0;
            if (this.longFunc) {
                this.longFunc()
            }
        }
    },


    update(dt) {
        if (this.holdClick) {
            this.holdTime++;
            if (this.holdTime > 120)//如果长按时间大于2s，则认为长按了2s
            {
                this.holdTime = 120;
            }
        }
    },

    onDestroy() {
        this.node.targetOff(this.node)
        if (this.setTime) {
            clearTimeout(this.setTime)
            this.setTime = null
        }

    },

});
